package org.example.studyonline.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studyonline.mapper.*;
import org.example.studyonline.pojo.Chapter;
import org.example.studyonline.pojo.Course;
import org.example.studyonline.pojo.DTO.*;
import org.example.studyonline.pojo.Product;
import org.example.studyonline.pojo.VO.ChapterVO;
import org.example.studyonline.pojo.VO.CourseListVO;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.VO.CourseVO;
import org.example.studyonline.pojo.VO.StatisticsVO;
import org.example.studyonline.service.CourseService;
import org.example.studyonline.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

@Service
public class CourseListImpl extends ServiceImpl<CourseMapper,Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    @Resource
    private ProductMapper productMapper;
    @Override
    public PageBean<CourseListVO> getCourseList(Long pageNum, Long pageSize) {

        IPage<CourseListVO> page = new Page<>(pageNum, pageSize);

        courseMapper.getCourseList(page,null);

        PageBean<CourseListVO> result = new PageBean<>();
        result.setPage(pageNum);
        result.setTotal(page.getPages());
        result.setItems(page.getRecords());

        return result;

    }

    @Override
    public CourseVO getChapters(Integer courseID) {
        // 先查课程信息
        Course course = courseMapper.selectById(courseID);
        Float price =productMapper.selectById(course.getProductID()).getPrice();
        if (ObjectUtils.isEmpty(course)){
            throw new RuntimeException("课程不存在");
        }
        CourseVO courseVO = BeanCopyUtils.copyBean(course, CourseVO.class);
        courseVO.setPrice(price);
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getCourseID,courseID);
        List<Chapter> chapters = chapterMapper.selectList(queryWrapper);
        List<ChapterVO> chapterVOS = BeanCopyUtils.copyBeanList(chapters, ChapterVO.class);
        courseVO.setChapters(chapterVOS);
        return courseVO;
    }

    @Override
    public void update(UpdateCourseDTO updateCourseDTO) {
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("courseID",updateCourseDTO.getCourseID());

        Course updateCourse = new Course();
        if (updateCourseDTO.getName()!=null&&!updateCourseDTO.getName().isEmpty()){
            updateCourse.setName(updateCourseDTO.getName());
        }
        if (updateCourseDTO.getDescription()!=null&&!updateCourseDTO.getDescription().isEmpty() ){
            updateCourse.setDescription(updateCourseDTO.getDescription());
        }
        if (updateCourseDTO.getImg()!=null&&!updateCourseDTO.getImg().isEmpty() ) {
            updateCourse.setImg(updateCourseDTO.getImg());
        }
        courseMapper.update(updateCourse,updateWrapper);

        Course course = courseMapper.selectById(updateCourseDTO.getCourseID());
        QueryWrapper<Product> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("productID",course.getProductID());
        Product product = productMapper.selectOne(queryWrapper);
        UpdateWrapper<Product> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("productID",product.getProductID());
        Product updateProduct = new Product();
        if (updateCourseDTO.getPrice()!=null&&!updateCourseDTO.getPrice().equals(product.getPrice())) {
            updateProduct.setPrice(updateCourseDTO.getPrice());
        }
        if (updateCourseDTO.getName()!=null&&!updateCourseDTO.getName().isEmpty()) {
            updateProduct.setName(updateCourseDTO.getName());
        }
        if (updateCourseDTO.getDescription()!=null&&!updateCourseDTO.getDescription().isEmpty()) {
            updateProduct.setDescription(updateCourseDTO.getDescription());
        }
        productMapper.update(updateProduct,updateWrapper1);

    }

    @Override
    public void add(AddCourseDTO addCourseDTO) {
        Product product = new Product();
        product.setPrice(addCourseDTO.getPrice());
        product.setName(addCourseDTO.getName());
        product.setDescription(addCourseDTO.getDescription());
        product.setType("COURSE");
        productMapper.insert(product);
        Integer productID = product.getProductID();

        Course newCourse = new Course();
        newCourse.setName(addCourseDTO.getName());
        newCourse.setDescription(addCourseDTO.getDescription());
        newCourse.setProductID(productID);
        newCourse.setImg(addCourseDTO.getImg());

        courseMapper.insert(newCourse);
    }

    @Override
    public void delete(DeleteCourseDTO deleteCourseDTO) {
        courseMapper.deleteById(deleteCourseDTO.getCourseID());
    }

    @Override
    public PageBean<StatisticsVO> getStatistics(Long pageNo,Long pageSize) {
        Long offset = (pageNo - 1) * pageSize;
        List<StatisticsVO> list = courseCategoryMapper.getCourseListByPage(offset,pageSize);
        long total = courseCategoryMapper.getCourseListTotal();
        PageBean<StatisticsVO> pageBean = new PageBean<>();
        pageBean.setPage(pageNo);
        pageBean.setTotal(total);
        pageBean.setItems(list);
        return pageBean;
    }

    @Override
    public PageBean<CourseListVO> searchCourse(String keyword, Long pageNo, Long pageSize) {
        QueryWrapper<CourseListVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("c.name",keyword);
        IPage<CourseListVO> page = new Page<>(pageNo, pageSize);

        courseMapper.getCourseList(page,queryWrapper);

        PageBean<CourseListVO> result = new PageBean<>();
        result.setPage(pageNo);
        result.setTotal(page.getPages());
        result.setItems(page.getRecords());

        return result;
    }

    @Override
    public PageBean<WatchCountDTO> getWatchOut(Long pageNum, Long pageSize) {
        IPage<WatchCountDTO> page = new Page<>(pageNum, pageSize);

        courseMapper.getWatchOut(page,null);

        PageBean<WatchCountDTO> result = new PageBean<>();
        result.setPage(pageNum);
        result.setTotal(page.getPages());
        result.setItems(page.getRecords());

        return result;
    }

    @Override
    public PageBean<CategoryCountDTO> getCategoryCount(Long pageNum, Long pageSize) {
        IPage<CategoryCountDTO> page = new Page<>(pageNum, pageSize);

        courseMapper.getCategoryCount(page,null);

        PageBean<CategoryCountDTO> result = new PageBean<>();
        result.setPage(pageNum);
        result.setTotal(page.getPages());
        result.setItems(page.getRecords());

        return result;
    }
}
